<?php
class ATag extends CActiveRecord
{
    public function InsertTag($tag, $type, $videoId)
    {
        $result = false;
        foreach($tag as $value){
            $sql = "SELECT id, name FROM c_tag WHERE name='" . mysql_escape_string($value) . "'";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();
            if(!empty($rows)){
                $tagId = $rows["id"];    
            } else {
                $sql = "INSERT INTO c_tag SET name=:name";
                $command = Yii::app()->db->createCommand($sql);
                $command->bindValue(":name", $value, PDO::PARAM_STR);                
                $result = $command->execute();    
                
                $tagId = Yii::app()->db->getLastInsertID();
            }
            
            $sql = "INSERT INTO c_tag_data SET 
                type=:type
                , dataId=:dataId
                , tagId=:tagId        
            ";
            $command = Yii::app()->db->createCommand($sql);
            $command->bindValue(":type", $type, PDO::PARAM_STR);
            $command->bindValue(":dataId", $videoId, PDO::PARAM_STR);
            $command->bindValue(":tagId", $tagId, PDO::PARAM_STR);
            $result = $command->execute();
        }
        
        return $result;        
    }
    
    public function UpdateTag($tag, $type, $id)
    {
        $sql = "DELETE FROM c_tag_data WHERE type=" . $type . " AND dataId=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();
        
        if(!empty($tag)){
            $tag = explode(";", $tag);
            
            foreach($tag as $value){
                $sql = "SELECT id, name FROM c_tag WHERE name='" . mysql_escape_string($value) . "'";
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryRow();
                if(!empty($rows)){
                    $tagId = $rows["id"];    
                } else {
                    $sql = "INSERT INTO c_tag SET name=:name";
                    $command = Yii::app()->db->createCommand($sql);
                    $command->bindValue(":name", $value, PDO::PARAM_STR);                
                    $result = $command->execute();    
                    
                    $tagId = Yii::app()->db->getLastInsertID();
                }
                
                $sql = "INSERT INTO c_tag_data SET 
                    type=:type
                    , dataId=:dataId
                    , tagId=:tagId        
                ";
                $command = Yii::app()->db->createCommand($sql);
                $command->bindValue(":type", $type, PDO::PARAM_STR);
                $command->bindValue(":dataId", $id, PDO::PARAM_STR);
                $command->bindValue(":tagId", $tagId, PDO::PARAM_STR);
                $result = $command->execute();
            }   
        }
        
        return $result;    
    }   
}  
?>
